﻿package org.demo.book;

import java.util.List;

import org.demo.book.BookGameConf;
import org.demo.book.BookImageUtil;
import org.demo.book.BookInstance;

/**
 * Description: 抽象board类
 * <br/>site: <a href="http://weibo.com/u/1679839007">nonoob</a> 
 * <br/>Copyright (C), 1989-2011, Hongxu Chen
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:2011-11-15
 * @author  Hongxu Chen leftcopy.chx@gmail.com
 * @version  1.0
 */

public abstract class BookAbstractBoard
{
	// 定义一个抽象方法, 让子类去实现
	protected abstract List<BookInstance> createBooks(BookGameConf config,
		BookInstance[][] books);

	public BookInstance[][] create(BookGameConf config)
	{
		// 创建BookInstance[][]数组
		BookInstance[][] books = new BookInstance[config.getXSize()][config.getYSize()];
		// 返回非空的BookInstance集合, 该集合由子类去创建
		List<BookInstance> notNullBooks = createBooks(config, books);
		// 根据非空Piece对象的集合的大小来取图片
		List<BookImage> playImages = BookImageUtil.getPlayImages(config.getContext(),
			notNullBooks.size());
		int imageWidth = playImages.get(0).getImage().getWidth();
		int imageHeight = playImages.get(0).getImage().getHeight();
		// 遍历非空的Piece集合
		for (int i = 0; i < notNullBooks.size(); i++)
		{
			// 依次获取每个BookInstance对象
			BookInstance book = notNullBooks.get(i);
			book.setImage(playImages.get(i));
			// 计算每本书左上角的X、Y坐标
			book.setBeginX(book.getIndexX() * imageWidth
				+ config.getBeginImageX());
			book.setBeginY(book.getIndexY() * imageHeight
				+ config.getBeginImageY());
			// 将该书对象放入方块数组的相应位置处
			books[book.getIndexX()][book.getIndexY()] = book;
		}
		return books;
	}
}
